<script type="text/javascript">
	$(document).ready(function() {
		$(".refresh-link").click(function() {
			//alert($(this).attr("href"));
			window.location.reload(true);
			return false;
		});
		
		function loadFromDoppel() {
			<!--alert($(this).attr("href"));-->
			//console.log(localStorage["hello.c"]);
			$("#dopple-form").empty();
			var file = new Object();
			
			//set attempte and question
			file.attempt = <?php echo $state->attempt;?>;
			file.question = <?php echo $state->question;?>;
			
			var key_prefix = "doppel-ganger-timotius-";
			
			//add all files
			file.files = new Object();
			
			for(var i=0; i<localStorage.length; ++i){ //Get all localStorage content with key prefixed with 'key_prefix'
				var key = localStorage.key(i);
				if(key.indexOf(key_prefix) != -1){
					file.files[key] = localStorage[key];
					var temp = file.files[key].replace(/\'/g,'%27');
					console.log(temp);
					
					$("#dopple-form").append('<input name="selected['+key+']" type="checkbox" />' + key.substring(key_prefix.length , key.length) + '<br />');
					$("#dopple-form").append('<input type="hidden" name="doppel['+key+']" value="'+encodeURIComponent(temp)+'" />');
				}
			}
			console.log(file);
		}
		
		$("#pick-oddysseus-file-<?php echo $evaluationset_id;?>").click(function() {
			loadFromDoppel();
			$("#dopple-area").toggle();
			
			///Put code here
			//POST to receiveoddysseus.php
			/*$.ajax({
                type: 'POST',
                url: '/moodle/question/type/sourcecode/receiveoddysseus.php',
				data: file,
                success: function(data){
                    console.log('data', data);
                },
                error: function(jqXHR, textStatus, errorThrown){
                    console.log('error', jqXHR, textStatus, errorThrown);
                }
            });*/
			
			return false;
		});
	});
</script>
<div class="qtext">
    <?php echo $questiontext; ?>
</div>
<?php if ($image) { ?>
    <img class="qimage" src="<?php echo $image; ?>" alt="" />
<?php } ?>

<div class="ablock clearfix">
    <!-- TODO add the form controls that the student will use to enter their response. -->
    <?php if (empty($options->readonly)) : ?>
    <div>
		Source code archive: <input type="file" name="solutionfile[<?php echo $state->question;?>]" /><br />
    </div>
	<hr />
	<button id="pick-oddysseus-file-<?php echo $evaluationset_id;?>">Get From DoppelGanger</button>
	<div id="dopple-area" style="display: none;">
		<div id="dopple-file-selector">
		</div>
		<div id="dopple-form">
		</div>
	</div>
    <?php endif;?>
    <hr />
    <table cellpadding="5px">
		<thead>
			<tr>
				<th valign="top">Time</th>
				<th valign="top">Name</th>
				<th valign="top">Grader Feedback</th>
			</tr>
		</thead>
		<tbody>
			<?php if ($submittedFiles != null) : ?>
				<?php foreach ($submittedFiles as $f) : ?>
				<tr>
					<td valign="top">
						<a href="<?php echo $CFG->wwwroot;?>/question/type/sourcecode/download.php?id=<?php echo $f->id?>"><?php echo $f->timestamp;?></a>
					</td>
					<td valign="top">
						<a href="<?php echo $CFG->wwwroot;?>/question/type/sourcecode/download.php?id=<?php echo $f->id?>"><?php echo $f->name;?></a>
					</td>
					<td>
						<a href="<?php echo $f->id;?>" class="refresh-link" id="refresh-<?php echo $f->id?>">Refresh</a>
						<?php if (isset($f->gradingResult)) : ?>
						<?php $gradingResult = $f->gradingResult;?>
						<table cellpadding="10px" valign="top">
							<thead>
								<tr>
									<th>Info</th>
									<th>Detail</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>Compile Result</td>
									<td><pre><?php echo $gradingResult['detail']['report']['compile_result']['output'];?></pre></td>
								</tr>
								<tr>
									<td>Score</td>
									<td><pre><?php echo $gradingResult['detail']['report']['grade_result']['score'];?></pre></td>
								</tr>
								<tr>
									<td>Detailed Result</td>
									<td>
										<table cellpadding="10px">
											<?php $details = $gradingResult['detail']['report']['grade_result']['detailedresult'];?>
											<?php foreach ($details as $key => $detail) : ?>
												<tr>
													<td valign="top"><pre><?php echo $key;?></pre></td>
													<td><pre><?php echo $detail;?></pre></td>
												</tr>
											<?php endforeach;?>
										</table>
									</td>
								</tr>
							</tbody>
						</table>
						<?php endif;?>
					</td>
				</tr>
				<?php endforeach;?>
			<?php endif;?>
		</tbody>
    </table>
    <?php if ($feedback) { ?>
        <div class="feedback">
            <?php echo $feedback ?>
        </div>
    <?php } ?>
    <?php if ($gradingResult != NULL) : ?>
    
    <?php endif;?>

    <?php $this->print_question_submit_buttons($question, $state, $cmoptions, $options); ?>
    <!--<a href="oddysseus-storage-<?php echo $evaluationset_id;?>" id="pick-oddysseus-file-<?php echo $evaluationset_id;?>">Get From DoppleGanger</a>-->
</div>
